from flask import Flask,request,render_template,session,json
from application.user import user
from application import colleague,schedule,events
from db_util import db
from db_util.models import User,Colleague
import uuid
import logging

#######################################
#              定义路由
#######################################
# 用户首页
@user.route('/')
@user.route('/user/')
def home():
    return render_template("user/home.html")


#跳转-用户登录界面
@user.route("/user/to_login_page")
def to_login_page():
    return render_template("user/login.html")

#跳转-用户注册界面
@user.route("/user/to_regist_page")
def to_regist_page():
    return render_template("user/regist.html")

#跳转添加同事界面
@user.route("/user/to_add_colleague")
def to_add_colleague():
    return render_template("user/add_colleague.html")

#跳转用户首页
@user.route("/user/to_user_index_page")
def to_user_index_page():
    return render_template("user/user_index.html")

#跳转用户待办界面
@user.route("/user/to_user_todolist_page")
def to_user_todolist_page():
    return render_template("user/user_todolist.html")

#######################################
#         定义接口:用户模块
#######################################
#用户登录
@user.route("/user/user_login",methods=['GET','POST'])
def user_login():
    try:
        username = request.form.get('username')
        password = request.form.get('password')

        user_judge = db.session.query(User).filter(User.username==username).all()
        if user_judge:
            if user_judge[0].password == password:
                #将用户信息保存到session中、
                session["login"]=1
                session["user_id"] = user_judge[0].id
                session["user_name"] = user_judge[0].username
                session["user_pwd"] = user_judge[0].password
                session["user_sex"] = user_judge[0].sex
                session["user_nickname"] = user_judge[0].nickname
                session["user_email"] = user_judge[0].email
                session["user_phone"] = user_judge[0].phone
                session["random"]=''
                session["colleague_id"]=''
                logging.debug("登录成功")
                colleague_list = colleague.views.colleague_list()
                return render_template("user/user_index.html",
                                       colleague_list = colleague_list)
            else:
                return render_template("user/login.html",login_res = 0);
        else:
            return render_template("user/login.html",login_res = 0);
    finally:
        db.session.close();

#用户注册
@user.route("/user/user_regist",methods=['GET','POST'])
def user_regist():
    try:
        user = User()
        user.username = request.form.get('username')
        user.password = request.form.get('password')
        user.nickname = request.form.get('nickname')
        user.sex = request.form.get('sex')
        user.email = request.form.get('email')
        user.phone = request.form.get('phone')
        user_judge = db.session.query(User).filter(User.username == user.username).all()
        if user_judge:
            db.session.close();
            return("用户名已存在")
        else:
            user_list = db.session.query(User).all();
            user.id = uuid.uuid1().hex;
            for item in user_list:
                colleague = Colleague()
                colleague.user_id = item.id;
                colleague.colleague_id = user.id;
                colleague.permission = 0;
                colleague.remark = "同事";
                db.session.add(colleague);
                db.session.commit();
                colleague2 = Colleague();
                colleague2.user_id = user.id;
                colleague2.colleague_id = item.id;
                colleague2.remark = "同事";
                colleague2.permission = 0;
                db.session.add(colleague2);
                db.session.commit();
                # db.session.close();
            db.session.add(user)
            db.session.commit()
            logging.debug("注册成功")
            return render_template("user/login.html",regist_res = 1);
    finally:
        db.session.close();

#获取用户资料
@user.route("/user/user_data_get",methods=['GET','POST'])
def user_data_get():
    user = User()
    user.id = session["user_id"]
    user_judge = db.session.query(User).filter(User.id == user.id).all()
    if user_judge:
        user.username=user_judge[0].username
        user.password=user_judge[0].password
        user.nickname = user_judge[0].nickname
        user.sex = user_judge[0].sex
        user.email = user_judge[0].email
        user.phone = user_judge[0].phone
        db.session.close();
        return(user.nickname+"：信息获取成功")
    else:
        db.session.close();
        return("信息获取失败")

#修改用户资料
@user.route("/user/user_data_update",methods=['GET','POST'])
def user_data_update():
    data = request.get_data("data")
    data = bytes.decode(data)
    if (len(data) > 0):
        data = json.loads(data)

    user = User();
    user.id = session['user_id'];
    user.username = data['user_name'];
    user.password = data['user_pwd'];
    user.nickname = data['user_nickname'];
    user.sex = data['user_sex'];
    user.email = data['user_email'];
    user.phone = data['user_phone'];
    session["login"] = 1
    session["user_id"] = user.id
    session["user_name"] = user.username
    session["user_pwd"] = user.password
    session["user_sex"] = user.sex
    session["user_nickname"] = user.nickname
    session["user_email"] = user.email
    session["user_phone"] = user.phone

    # if session["user_id"]==0:
    #     return("用户尚未登录")
    # user.id = session["user_id"]
    db.session.merge(user)
    db.session.commit()
    db.session.close();
    return ("修改用户信息成功！")

#用户注销
@user.route("/user/user_logout",methods=['GET','POST'])
def user_logout():
    session.clear()
    session["login"] = 0
    return render_template("/user/home.html")


def login_judge():
    if session["user_id"]:
        if session["user_id"]!=0:
            return True
    else:
        return False
